package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowMessageListVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowMessageUpdateStatusVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowMessageVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowMessageRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowMessage;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowMessageExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowMessageDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lilh
 * @date 2020/11/30 下午3:23
 */
@Repository
public class FlowMessageRepositoryImpl implements FlowMessageRepository {

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private FlowMessageDAO flowMessageDAO;

    @Autowired
    private UserRepository userRepository;

    @Override
    public boolean insertMessage(FlowMessageVo messageVo) {
        if (Objects.isNull(messageVo)) {
            return false;
        }
        messageVo.setId(idGenerator.getNextId());
        return flowMessageDAO.insertSelective(convert(messageVo)) > 0;
    }

    @Override
    public FlowMessageListVo detail(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        FlowMessage flowMessage = flowMessageDAO.selectByPrimaryKey(id);
        FlowMessageVo flowMessageVo=new FlowMessageVo();
        BeanUtils.copyProperties(flowMessage,flowMessageVo);
        FlowMessageListVo flowMessageListVo=new FlowMessageListVo(flowMessageVo);
        return flowMessageListVo;
    }

    @Override
    public ResponseResult list(FlowMessageVo messageVo) {
        User current = userRepository.getCurrentUser();
        FlowMessageExample query = new FlowMessageExample();
        FlowMessageExample.Criteria criteria = query.createCriteria().andReceiverCodeEqualTo(current.getUserCode());
        if (StringUtils.isNotEmpty(messageVo.getProcessName())) {
            criteria.andMsgTitleLike("%" + messageVo.getProcessName() + "%");
        }
        if (StringUtils.isNotEmpty(messageVo.getMsgType())) {
            criteria.andMsgTypeEqualTo(messageVo.getMsgType());
        }
        if (StringUtils.isNotEmpty(messageVo.getSenderName())) {
            criteria.andProcessNameLike("%" + messageVo.getSenderName() + "%");
        }
        if (messageVo.getStatus() != null) {
            criteria.andStatusEqualTo(messageVo.getStatus());
        }

        query.setOrderByClause("status asc ,create_time desc");
        PageHelper.startPage(messageVo.getPageNum(), messageVo.getPageSize(), true);
        List<FlowMessage> flowMessages = flowMessageDAO.selectByExampleWithBLOBs(query);
        PageInfo<FlowMessage> pageInfoTotal = new PageInfo<>(flowMessages);
        PageInfo<FlowMessageVo> pageInfo = new PageInfo<>(flowMessages.stream().map(this::convert).collect(Collectors.toList()));
        List<FlowMessageListVo> result = pageInfo.getList().stream().map(FlowMessageListVo::new).collect(Collectors.toList());
        return ResponseResult.success().setTotalCount((int) pageInfoTotal.getTotal()).setPageNum(pageInfo.getPageNum())
                .setPageSize(pageInfo.getPageSize()).setDatas(result);
    }

    @Override
    public int updateReadStatus(FlowMessageUpdateStatusVo flowMessageUpdateStatusVo) {
        FlowMessageExample query = new FlowMessageExample();
        FlowMessageExample.Criteria criteria = query.createCriteria().andReceiverCodeEqualTo(userRepository.getCurrentUser().getUserCode()).andStatusEqualTo(0);
        if (CollectionUtils.isNotEmpty(flowMessageUpdateStatusVo.getIds())) {
            //更新指定的记录
            criteria.andIdIn(flowMessageUpdateStatusVo.getIds());
        }
        FlowMessage entity = new FlowMessage();
        entity.setStatus(1);
        entity.setUpdateTime(new Date());

        return flowMessageDAO.updateByExampleSelective(entity, query);
    }

    private FlowMessage convert(FlowMessageVo messageVo) {
        if (Objects.isNull(messageVo)) {
            return null;
        }

        FlowMessage result = new FlowMessage();
        BeanUtils.copyProperties(messageVo, result);
        return result;
    }

    private FlowMessageVo convert(FlowMessage message) {
        if (Objects.isNull(message)) {
            return null;
        }

        FlowMessageVo result = new FlowMessageVo();
        BeanUtils.copyProperties(message, result);
        return result;
    }
}
